<?php

namespace App\Listeners\Subscriber;

use Exception;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Events\Department\DeletedEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class DepartmentEventSubscriber
{
	/**
	 * 删除部门后触发逻辑
	 *
	 * Date: 16/05/2018
	 * @author George
	 * @param DeletedEvent $event
	 */
	protected function onDeleted(DeletedEvent $event)
	{
		try {
			DB::table('users')
				->where('department_id', $event->department->id)
				->update(['department_id' => 0]);
		} catch (Exception $exception) {
			Log::error($exception->getMessage());
		}
	}

	/**
	 * 定义时间订阅者
	 *
	 * Date: 16/05/2018
	 * @author George
	 * @param Dispatcher $events
	 */
	public function subscribe(Dispatcher $events)
	{
		$events->listen(
			'App\Events\Department\DeletedEvent',
			'App\Listeners\Subscriber\DepartmentEventSubscriber@onDeleted'
		);
    }
}
